from django.db import models

# Create your models here.

class Author(models.Model):
    '''作家模型类'''
    name = models.CharField('姓名', max_length=11)

class Wife(models.Model):
    '''作家妻子模型类'''
    name = models.CharField('姓名', max_length=11)
    author = models.OneToOneField(Author, on_delete=models.CASCADE)    #增加1对1属性

# D:\PyCharm\pycharm_project\new\mysite1>python manage.py shell
# Python 3.5.3 (v3.5.3:1880cb95a742, Jan 16 2017, 16:02:32) [MSC v.1900 64 bit (AMD64)] on win32
# Type "help", "copyright", "credits" or "license" for more information.
# (InteractiveConsole)
# >>> from oto.models import *
# >>> a1 = Author.objects.create(name='wang')
# >>> w1 = Wife.objects.create(name='wangfuren',author=a1)
# >>> a2 = Author.objects.create(name='guo')
# >>> w2 = Wife.objects.create(name='guofuren',author_id=2)
# mysql> select * from oto_author;
# +----+------+
# | id | name |
# +----+------+
# |  1 | wang |
# |  2 | guo  |
# +----+------+
# 2 rows in set (0.00 sec)
# mysql> select * from oto_wife;
# +----+-----------+-----------+
# | id | name      | author_id |
# +----+-----------+-----------+
# |  1 | wangfuren |         1 |
# |  2 | guofuren  |         2 |
# +----+-----------+-----------+
# 2 rows in set (0.00 sec)
#正向查询
# >>> w2
# <Wife: Wife object (2)>
# >>> w2.author
# <Author: Author object (2)>
# >>> w2.author.name
# 'guo'
#反向查询
# >>> a2
# <Author: Author object (2)>
# >>> a2.wife
# <Wife: Wife object (2)>
# >>> a2.wife.name
# 'guofuren'





